package com.sponsorpay.sdk.android.publisher.mbe;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Handler;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager.BadTokenException;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout.LayoutParams;
import android.widget.Toast;
import com.sponsorpay.sdk.android.SponsorPay;
import com.sponsorpay.sdk.android.credentials.SPCredentials;
import com.sponsorpay.sdk.android.publisher.OfferWebClient;
import com.sponsorpay.sdk.android.publisher.SponsorPayPublisher;
import com.sponsorpay.sdk.android.publisher.SponsorPayPublisher.UIStringIdentifier;
import com.sponsorpay.sdk.android.publisher.currency.SPCurrencyServerListener;
import com.sponsorpay.sdk.android.utils.SponsorPayBaseUrlProvider;
import com.sponsorpay.sdk.android.utils.SponsorPayLogger;
import com.sponsorpay.sdk.android.utils.UrlBuilder;
import java.lang.reflect.Method;
import java.util.Map;

public class SPBrandEngageClient
{
  private static final String ABOUT_BLANK = "about:blank";
  public static final SPBrandEngageClient INSTANCE = new SPBrandEngageClient();
  private static final String MBE_URL_KEY = "mbe";
  public static final String SP_ENGAGEMENT_STATUS = "ENGAGEMENT_STATUS";
  private static final String SP_NUMBER_OF_OFFERS_PARAMETER_KEY = "n";
  private static final String SP_REQUEST_OFFER_ANSWER = "requestOffers";
  private static final String SP_REQUEST_START_STATUS = "start";
  public static final String SP_REQUEST_STATUS_PARAMETER_ABORTED_VALUE = "CLOSE_ABORTED";
  private static final String SP_REQUEST_STATUS_PARAMETER_ENGAGED = "USER_ENGAGED";
  public static final String SP_REQUEST_STATUS_PARAMETER_ERROR = "ERROR";
  public static final String SP_REQUEST_STATUS_PARAMETER_FINISHED_VALUE = "CLOSE_FINISHED";
  private static final String SP_REQUEST_STATUS_PARAMETER_KEY = "status";
  private static final String SP_REQUEST_STATUS_PARAMETER_STARTED_VALUE = "STARTED";
  private static final String SP_START_ENGAGEMENT = "javascript:Sponsorpay.MBE.SDKInterface.do_start()";
  private static final String TAG = "SPBrandEngageClient";
  private static final int TIMEOUT = 10000;
  private static final int VCS_TIMEOUT = 3000;
  private Activity mActivity;
  private WebChromeClient mChromeClient;
  private Context mContext;
  private String mCurrency;
  private Map<String, String> mCustomParameters;
  private Handler mHandler = new Handler();
  private BroadcastReceiver mNetworkStateReceiver = new BroadcastReceiver()
  {
    public void onReceive(Context paramAnonymousContext, Intent paramAnonymousIntent)
    {
      boolean bool = paramAnonymousIntent.getBooleanExtra("noConnectivity", false);
      int i = 0;
      if (bool);
      while (true)
      {
        if ((i == 0) && (SPBrandEngageClient.this.mStatus == SPBrandEngageOffersStatus.SHOWING_OFFERS))
        {
          SponsorPayLogger.e("SPBrandEngageClient", "Connection has been lost");
          SPBrandEngageClient.this.mHandler.post(new Runnable()
          {
            public void run()
            {
              SPBrandEngageClient.this.showErrorDialog(SponsorPayPublisher.getUIString(SponsorPayPublisher.UIStringIdentifier.MBE_ERROR_DIALOG_MESSAGE_OFFLINE));
            }
          });
        }
        return;
        i = 1;
      }
    }
  };
  private View.OnTouchListener mOnTouchListener;
  private boolean mShowRewardsNotification = true;
  private boolean mShowingDialog = false;
  private SPBrandEngageOffersStatus mStatus = SPBrandEngageOffersStatus.MUST_QUERY_SERVER_FOR_OFFERS;
  private SPBrandEngageClientStatusListener mStatusListener;
  private SPCurrencyServerListener mVCSListener;
  private WebViewClient mWebClient;
  private WebView mWebView;

  private boolean canChangeParameters()
  {
    return this.mStatus.canChangeParameters();
  }

  private void changeStatus(String paramString)
  {
    if (paramString.equals("STARTED"))
    {
      setClientStatus(SPBrandEngageOffersStatus.SHOWING_OFFERS);
      notifyListener(SPBrandEngageClientStatusListener.SPBrandEngageClientStatus.STARTED);
    }
    do
    {
      return;
      if (paramString.equals("CLOSE_FINISHED"))
      {
        clearWebViewPage();
        notifyListener(SPBrandEngageClientStatusListener.SPBrandEngageClientStatus.CLOSE_FINISHED);
        showRewardsNotification();
        checkForCoins();
        return;
      }
      if (paramString.equals("CLOSE_ABORTED"))
      {
        clearWebViewPage();
        notifyListener(SPBrandEngageClientStatusListener.SPBrandEngageClientStatus.CLOSE_ABORTED);
        return;
      }
      if (paramString.equals("ERROR"))
      {
        showErrorDialog(SponsorPayPublisher.getUIString(SponsorPayPublisher.UIStringIdentifier.MBE_ERROR_DIALOG_MESSAGE_DEFAULT));
        return;
      }
    }
    while (!paramString.equals("USER_ENGAGED"));
    setClientStatus(SPBrandEngageOffersStatus.USER_ENGAGED);
  }

  private void checkEngagementStarted()
  {
    Runnable local3 = new Runnable()
    {
      public void run()
      {
        if ((SPBrandEngageClient.this.mStatus != SPBrandEngageOffersStatus.SHOWING_OFFERS) && (SPBrandEngageClient.this.mStatus != SPBrandEngageOffersStatus.USER_ENGAGED) && (SPBrandEngageClient.this.mWebView != null))
          SPBrandEngageClient.this.showErrorDialog(SponsorPayPublisher.getUIString(SponsorPayPublisher.UIStringIdentifier.MBE_ERROR_DIALOG_MESSAGE_DEFAULT));
      }
    };
    this.mHandler.postDelayed(local3, 10000L);
  }

  private void checkForCoins()
  {
    if (this.mVCSListener != null)
      this.mHandler.postDelayed(new Runnable()
      {
        public void run()
        {
          try
          {
            SponsorPayPublisher.requestNewCoins(SponsorPay.getCurrentCredentials().getCredentialsToken(), SPBrandEngageClient.this.mContext, SPBrandEngageClient.this.mVCSListener, null, null, SPBrandEngageClient.this.mCurrency);
            return;
          }
          catch (RuntimeException localRuntimeException)
          {
            SponsorPayLogger.e("SPBrandEngageClient", "Error in VCS request", localRuntimeException);
          }
        }
      }
      , 3000L);
  }

  private void clearWebViewPage()
  {
    if (this.mWebView != null)
      this.mWebView.loadUrl("about:blank");
    if ((this.mStatus == SPBrandEngageOffersStatus.SHOWING_OFFERS) || (this.mStatus == SPBrandEngageOffersStatus.USER_ENGAGED) || (this.mStatus == SPBrandEngageOffersStatus.READY_TO_SHOW_OFFERS))
      this.mContext.unregisterReceiver(this.mNetworkStateReceiver);
    this.mWebView = null;
    this.mActivity = null;
    setClientStatus(SPBrandEngageOffersStatus.MUST_QUERY_SERVER_FOR_OFFERS);
  }

  private String getBaseUrl()
  {
    return SponsorPayBaseUrlProvider.getBaseUrl("mbe");
  }

  private View.OnTouchListener getOnTouchListener()
  {
    if (this.mOnTouchListener == null)
    {
      GestureDetector.OnDoubleTapListener local8 = new GestureDetector.OnDoubleTapListener()
      {
        public boolean onDoubleTap(MotionEvent paramAnonymousMotionEvent)
        {
          SponsorPayLogger.d("SPBrandEngageClient", "double tap event");
          return true;
        }

        public boolean onDoubleTapEvent(MotionEvent paramAnonymousMotionEvent)
        {
          SponsorPayLogger.d("SPBrandEngageClient", "double tap event");
          return true;
        }

        public boolean onSingleTapConfirmed(MotionEvent paramAnonymousMotionEvent)
        {
          return false;
        }
      };
      final GestureDetector localGestureDetector = new GestureDetector(new GestureDetector.OnGestureListener()
      {
        public boolean onDown(MotionEvent paramAnonymousMotionEvent)
        {
          return false;
        }

        public boolean onFling(MotionEvent paramAnonymousMotionEvent1, MotionEvent paramAnonymousMotionEvent2, float paramAnonymousFloat1, float paramAnonymousFloat2)
        {
          return false;
        }

        public void onLongPress(MotionEvent paramAnonymousMotionEvent)
        {
        }

        public boolean onScroll(MotionEvent paramAnonymousMotionEvent1, MotionEvent paramAnonymousMotionEvent2, float paramAnonymousFloat1, float paramAnonymousFloat2)
        {
          return false;
        }

        public void onShowPress(MotionEvent paramAnonymousMotionEvent)
        {
        }

        public boolean onSingleTapUp(MotionEvent paramAnonymousMotionEvent)
        {
          return false;
        }
      });
      localGestureDetector.setOnDoubleTapListener(local8);
      this.mOnTouchListener = new View.OnTouchListener()
      {
        public boolean onTouch(View paramAnonymousView, MotionEvent paramAnonymousMotionEvent)
        {
          return localGestureDetector.onTouchEvent(paramAnonymousMotionEvent);
        }
      };
    }
    return this.mOnTouchListener;
  }

  private WebChromeClient getWebChromeClient()
  {
    if (this.mChromeClient == null)
      this.mChromeClient = new WebChromeClient()
      {
        private void showJSDialog(String paramAnonymousString1, String paramAnonymousString2)
        {
          if (!SPBrandEngageClient.this.mShowingDialog)
          {
            SPBrandEngageClient.this.mShowingDialog = true;
            if (SPBrandEngageClient.this.mActivity != null)
              break label106;
          }
          label106: for (Object localObject = SPBrandEngageClient.this.mContext; ; localObject = SPBrandEngageClient.this.mActivity)
          {
            AlertDialog.Builder localBuilder = new AlertDialog.Builder((Context)localObject);
            localBuilder.setTitle(SponsorPayPublisher.getUIString(SponsorPayPublisher.UIStringIdentifier.MBE_FORFEIT_DIALOG_TITLE)).setMessage(paramAnonymousString2).setPositiveButton("OK", new DialogInterface.OnClickListener()
            {
              public void onClick(DialogInterface paramAnonymous2DialogInterface, int paramAnonymous2Int)
              {
                SPBrandEngageClient.this.changeStatus("CLOSE_ABORTED");
                SPBrandEngageClient.this.mShowingDialog = false;
              }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener()
            {
              public void onClick(DialogInterface paramAnonymous2DialogInterface, int paramAnonymous2Int)
              {
                SPBrandEngageClient.this.mShowingDialog = false;
              }
            }).setOnCancelListener(new DialogInterface.OnCancelListener()
            {
              public void onCancel(DialogInterface paramAnonymous2DialogInterface)
              {
                SPBrandEngageClient.this.mShowingDialog = false;
              }
            });
            localBuilder.show();
            return;
          }
        }

        public boolean onJsConfirm(WebView paramAnonymousWebView, String paramAnonymousString1, String paramAnonymousString2, JsResult paramAnonymousJsResult)
        {
          showJSDialog(paramAnonymousString1, paramAnonymousString2);
          paramAnonymousJsResult.cancel();
          return true;
        }
      };
    return this.mChromeClient;
  }

  private WebViewClient getWebClient()
  {
    if (this.mWebClient == null)
      this.mWebClient = new OfferWebClient(this.mActivity)
      {
        protected Activity getHostActivity()
        {
          return SPBrandEngageClient.this.mActivity;
        }

        protected void onPlayStoreNotFound()
        {
          SPBrandEngageClient.this.showErrorDialog(SponsorPayPublisher.getUIString(SponsorPayPublisher.UIStringIdentifier.ERROR_PLAY_STORE_UNAVAILABLE));
        }

        public void onReceivedError(WebView paramAnonymousWebView, int paramAnonymousInt, String paramAnonymousString1, String paramAnonymousString2)
        {
          SponsorPayLogger.d("SPBrandEngageClient", "onReceivedError url - " + paramAnonymousString2 + " - " + paramAnonymousString1);
          SPBrandEngageClient.this.showErrorDialog(SponsorPayPublisher.getUIString(SponsorPayPublisher.UIStringIdentifier.MBE_ERROR_DIALOG_MESSAGE_DEFAULT));
          super.onReceivedError(paramAnonymousWebView, paramAnonymousInt, paramAnonymousString1, paramAnonymousString2);
        }

        protected void onSponsorPayExitScheme(int paramAnonymousInt, String paramAnonymousString)
        {
          Activity localActivity = getHostActivity();
          if (localActivity == null)
            return;
          localActivity.setResult(paramAnonymousInt);
          launchActivityWithUrl(paramAnonymousString);
        }

        protected void onTargetActivityStart(String paramAnonymousString)
        {
          SPBrandEngageClient.this.changeStatus("USER_ENGAGED");
          SPBrandEngageClient.this.notifyListener(SPBrandEngageClientStatusListener.SPBrandEngageClientStatus.PENDING_CLOSE);
        }

        protected void processSponsorPayScheme(String paramAnonymousString, Uri paramAnonymousUri)
        {
          if (paramAnonymousString.equals("requestOffers"))
            SPBrandEngageClient.this.processQueryOffersResponse(Integer.parseInt(paramAnonymousUri.getQueryParameter("n")));
          while (!paramAnonymousString.equals("start"))
            return;
          SPBrandEngageClient.this.changeStatus(paramAnonymousUri.getQueryParameter("status"));
        }

        public boolean shouldOverrideUrlLoading(WebView paramAnonymousWebView, String paramAnonymousString)
        {
          if (paramAnonymousString.contains("youtube.com"))
          {
            SponsorPayLogger.d("SPBrandEngageClient", "Preventin the opening of Youtube app");
            return true;
          }
          return super.shouldOverrideUrlLoading(paramAnonymousWebView, paramAnonymousString);
        }
      };
    return this.mWebClient;
  }

  private void notifyListener(SPBrandEngageClientStatusListener.SPBrandEngageClientStatus paramSPBrandEngageClientStatus)
  {
    if (this.mStatusListener != null)
    {
      SponsorPayLogger.i("SPBrandEngageClient", "SPBrandEngageClientStatus -> " + paramSPBrandEngageClientStatus);
      this.mStatusListener.didChangeStatus(paramSPBrandEngageClientStatus);
    }
  }

  private void processQueryOffersResponse(int paramInt)
  {
    boolean bool;
    if (paramInt > 0)
    {
      bool = true;
      if (!bool)
        break label40;
      setClientStatus(SPBrandEngageOffersStatus.READY_TO_SHOW_OFFERS);
    }
    while (true)
    {
      if (this.mStatusListener != null)
        this.mStatusListener.didReceiveOffers(bool);
      return;
      bool = false;
      break;
      label40: setClientStatus(SPBrandEngageOffersStatus.MUST_QUERY_SERVER_FOR_OFFERS);
    }
  }

  private void setClientStatus(SPBrandEngageOffersStatus paramSPBrandEngageOffersStatus)
  {
    this.mStatus = paramSPBrandEngageOffersStatus;
    SponsorPayLogger.d("SPBrandEngageClient", "SPBrandEngageClient mStatus -> " + paramSPBrandEngageOffersStatus.name());
  }

  private void setupWebView(Activity paramActivity)
  {
    if (Build.VERSION.SDK_INT < 11);
    while (true)
    {
      this.mContext = paramActivity;
      this.mWebView = new WebView(this.mContext);
      this.mWebView.getSettings().setJavaScriptEnabled(true);
      this.mWebView.getSettings().setPluginsEnabled(true);
      this.mWebView.getSettings().setUseWideViewPort(false);
      this.mWebView.setBackgroundColor(0);
      if (Build.VERSION.SDK_INT < 14)
        this.mWebView.getSettings().setUserAgent(1);
      this.mWebView.setScrollBarStyle(0);
      this.mWebView.setWebChromeClient(getWebChromeClient());
      this.mWebView.setWebViewClient(getWebClient());
      this.mWebView.setOnTouchListener(getOnTouchListener());
      IntentFilter localIntentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
      this.mContext.registerReceiver(this.mNetworkStateReceiver, localIntentFilter);
      return;
      paramActivity = paramActivity.getApplicationContext();
    }
  }

  private void showErrorDialog(String paramString)
  {
    Object localObject;
    if ((!this.mShowingDialog) && (this.mWebView != null))
    {
      this.mShowingDialog = true;
      if (this.mActivity != null)
        break label78;
      localObject = this.mContext;
    }
    while (true)
    {
      AlertDialog.Builder localBuilder = new AlertDialog.Builder((Context)localObject);
      localBuilder.setTitle(SponsorPayPublisher.getUIString(SponsorPayPublisher.UIStringIdentifier.MBE_ERROR_DIALOG_TITLE)).setMessage(paramString).setNeutralButton(SponsorPayPublisher.getUIString(SponsorPayPublisher.UIStringIdentifier.MBE_ERROR_DIALOG_BUTTON_TITLE_DISMISS), new DialogInterface.OnClickListener()
      {
        public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
        {
          SPBrandEngageClient.this.notifyListener(SPBrandEngageClientStatusListener.SPBrandEngageClientStatus.ERROR);
          SPBrandEngageClient.this.clearWebViewPage();
          SPBrandEngageClient.this.mShowingDialog = false;
        }
      });
      try
      {
        localBuilder.show();
        return;
        label78: localObject = this.mActivity;
      }
      catch (WindowManager.BadTokenException localBadTokenException)
      {
        this.mShowingDialog = false;
        SponsorPayLogger.e("SPBrandEngageClient", "Unable to show the dialog window");
      }
    }
  }

  private void showRewardsNotification()
  {
    if (this.mShowRewardsNotification)
      Toast.makeText(this.mContext, SponsorPayPublisher.getUIString(SponsorPayPublisher.UIStringIdentifier.MBE_REWARD_NOTIFICATION), 1).show();
  }

  private void startQueryingOffers(SPCredentials paramSPCredentials)
  {
    String str = UrlBuilder.newBuilder(getBaseUrl(), paramSPCredentials).setCurrency(this.mCurrency).addExtraKeysValues(this.mCustomParameters).addScreenMetrics().buildUrl();
    SponsorPayLogger.d("SPBrandEngageClient", "Loading URL: " + str);
    this.mWebView.loadUrl(str);
    setClientStatus(SPBrandEngageOffersStatus.QUERYING_SERVER_FOR_OFFERS);
    Runnable local2 = new Runnable()
    {
      public void run()
      {
        if (SPBrandEngageClient.this.mStatus == SPBrandEngageOffersStatus.QUERYING_SERVER_FOR_OFFERS)
        {
          SponsorPayLogger.d("SPBrandEngageClient", "Timeout reached, canceling request...");
          SPBrandEngageClient.this.clearWebViewPage();
        }
      }
    };
    this.mHandler.postDelayed(local2, 10000L);
  }

  public boolean canRequestOffers()
  {
    return this.mStatus.canRequestOffers();
  }

  public boolean canStartEngagement()
  {
    return this.mStatus.canShowOffers();
  }

  public void closeEngagement()
  {
    if (this.mStatus == SPBrandEngageOffersStatus.USER_ENGAGED)
    {
      changeStatus("CLOSE_FINISHED");
      return;
    }
    changeStatus("CLOSE_ABORTED");
  }

  public void onPause()
  {
    this.mHandler.post(new Runnable()
    {
      public void run()
      {
        if (SPBrandEngageClient.this.mWebView != null);
        try
        {
          Class.forName("android.webkit.WebView").getMethod("onPause", null).invoke(SPBrandEngageClient.this.mWebView, null);
          return;
        }
        catch (Exception localException)
        {
          SponsorPayLogger.e("SPBrandEngageClient", "onPause error", localException);
        }
      }
    });
  }

  public boolean requestOffers(SPCredentials paramSPCredentials, Activity paramActivity)
  {
    if (canRequestOffers())
    {
      if (Build.VERSION.SDK_INT < 8)
        processQueryOffersResponse(0);
      while (true)
      {
        return true;
        if (this.mWebView == null)
          setupWebView(paramActivity);
        startQueryingOffers(paramSPCredentials);
      }
    }
    SponsorPayLogger.d("SPBrandEngageClient", "SPBrandEngageClient cannot request offers at this point. It might be requesting offers right now or an offer might be currently being presented to the user.");
    return false;
  }

  public boolean setCurrencyListener(SPCurrencyServerListener paramSPCurrencyServerListener)
  {
    if (canChangeParameters())
    {
      this.mVCSListener = paramSPCurrencyServerListener;
      setClientStatus(SPBrandEngageOffersStatus.MUST_QUERY_SERVER_FOR_OFFERS);
      return true;
    }
    SponsorPayLogger.d("SPBrandEngageClient", "Cannot change the currency listener while a request to the server is going on or an offer is being presented to the user.");
    return false;
  }

  public boolean setCurrencyName(String paramString)
  {
    if (canChangeParameters())
    {
      this.mCurrency = paramString;
      setClientStatus(SPBrandEngageOffersStatus.MUST_QUERY_SERVER_FOR_OFFERS);
      return true;
    }
    SponsorPayLogger.d("SPBrandEngageClient", "Cannot change the currency while a request to the server is going on or an offer is being presented to the user.");
    return false;
  }

  public boolean setCustomParameters(Map<String, String> paramMap)
  {
    if (canChangeParameters())
    {
      this.mCustomParameters = paramMap;
      setClientStatus(SPBrandEngageOffersStatus.MUST_QUERY_SERVER_FOR_OFFERS);
      return true;
    }
    SponsorPayLogger.d("SPBrandEngageClient", "Cannot change custom parameters while a request to the server is going on or an offer is being presented to the user.");
    return false;
  }

  public void setShowRewardsNotification(boolean paramBoolean)
  {
    this.mShowRewardsNotification = paramBoolean;
  }

  public boolean setStatusListener(SPBrandEngageClientStatusListener paramSPBrandEngageClientStatusListener)
  {
    boolean bool = canChangeParameters();
    if (bool)
    {
      this.mStatusListener = paramSPBrandEngageClientStatusListener;
      return bool;
    }
    SponsorPayLogger.d("SPBrandEngageClient", "Cannot change the status listener while a request to the server is going on or an offer is being presented to the user.");
    return bool;
  }

  public boolean shouldShowRewardsNotification()
  {
    return this.mShowRewardsNotification;
  }

  public boolean startEngagement(Activity paramActivity)
  {
    if (canStartEngagement())
    {
      this.mWebView.loadUrl("javascript:Sponsorpay.MBE.SDKInterface.do_start()");
      this.mActivity = paramActivity;
      this.mActivity.addContentView(this.mWebView, new FrameLayout.LayoutParams(-1, -1));
      checkEngagementStarted();
      return true;
    }
    SponsorPayLogger.d("SPBrandEngageClient", "SPBrandEngageClient is not ready to show offers. Call requestOffers() and wait until your listener is called with the confirmation that offers have been received.");
    return false;
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.sponsorpay.sdk.android.publisher.mbe.SPBrandEngageClient
 * JD-Core Version:    0.6.2
 */